/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.server.behaviours;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.vecmath.Vector2d;
import locusts.common.entities.Actor;
import locusts.common.entities.EntityList;

/**
 * BehaviourList is a special behaviour that allows a number of
 * behaviours to be combined. For example averaged.
 *
 * @author Hamish Morgan Morgan
 */
public abstract class AbstractBehaviourList extends AbstractBehaviour
        implements  BehaviourList {

    private final List<Behaviour> behaviours;

    protected AbstractBehaviourList(double weight, List<Behaviour> behaviours) {
        super(weight);
        this.behaviours = behaviours;
    }

    protected AbstractBehaviourList(double weight) {
        this(weight, new ArrayList<Behaviour>());
    }

    abstract public Vector2d getSteering(Actor boid);

    public Vector2d[] getForces(Actor boid) {
        Vector2d[] forces = new Vector2d[behaviours.size()];

        for (int i = 0; i < behaviours.size(); i++) {
            forces[i] = behaviours.get(i).getSteering(boid);
            if (Double.isNaN(forces[i].x) || Double.isNaN(forces[i].y) ||
                    Double.isInfinite(forces[i].x) || Double.isInfinite(
                    forces[i].x))
                throw new RuntimeException("Steering is NaN: {}" + forces[i] +
                        " class: " + behaviours.get(i).getClass());
        }
        return forces;

    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final AbstractBehaviourList other = (AbstractBehaviourList) obj;
        if (this.behaviours != other.behaviours &&
                (this.behaviours == null ||
                !this.behaviours.equals(other.behaviours))) return false;
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        int hash = super.hashCode();
        hash = 37 * hash +
                (this.behaviours != null ? this.behaviours.hashCode() : 0);
        return hash;
    }

    public boolean add(Behaviour arg0) {
        return behaviours.add(arg0);
    }

    public int size() {
        return behaviours.size();
    }

    public boolean isEmpty() {
        return behaviours.isEmpty();
    }

    public boolean contains(Object arg0) {
        return behaviours.contains((Behaviour) arg0);
    }

    public Iterator<Behaviour> iterator() {
        return behaviours.iterator();
    }

    public Object[] toArray() {
        return behaviours.toArray();
    }

    public <T> T[] toArray(T[] arg0) {
        return behaviours.toArray(arg0);
    }

    public boolean remove(Object arg0) {
        return behaviours.remove((Behaviour) arg0);
    }

    public boolean containsAll(Collection<?> arg0) {
        return behaviours.containsAll(arg0);
    }

    public boolean addAll(Collection<? extends Behaviour> arg0) {
        return behaviours.addAll(arg0);
    }

    public boolean removeAll(Collection<?> arg0) {
        return behaviours.removeAll(arg0);
    }

    public boolean retainAll(Collection<?> arg0) {
        return behaviours.retainAll(arg0);
    }

    public void clear() {
        behaviours.clear();
    }

    @Override
    public void initialize(EntityList entities) {
        for(Behaviour b : behaviours)
            b.initialize(entities);
    }

    @Override
    public void clean() {
        for(Behaviour b : behaviours)
            b.clean();
    }
}
